﻿using System.Collections.Generic;
using System.Linq;

using SIG.Model;
using SIG.Repository;

using SIG.Infrastructure.Logging;
using SIG.Infrastructure.Cache;
using SIG.Infrastructure.Configs;
using SIG.Resources;
using System.Linq.Expressions;
using System;
using SIG.Basic.Extensions;
using SIG.Model.Videos;
using SIG.Resources.Admin;

namespace SIG.Service
{
    public class VideoService: EntityService<Video>, IVideoService
    {
     
        private readonly IVideoRepository _specialistRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public VideoService(IVideoRepository specialistRepository, ILoggingService loggingService, ICacheService cacheService)
            : base(specialistRepository, cacheService, loggingService)
        {
           
            _specialistRepository = specialistRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }

        public IEnumerable<Video> GetActiveVideos()
        {
            //get list
            var key = $"{EntityNames.Video}_ActiveList";

            List<Video> specialists;
            if (SettingsManager.Video.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    specialists = (List<Video>)_cacheService.Get(key);

                }
                else
                {
                    specialists = _specialistRepository.GetFilteredElements(c=>c.Active).ToList();
                    _cacheService.Set(key, specialists, SettingsManager.Video.CacheDuration);
                }
            }
            else
            {
                specialists = _specialistRepository.GetFilteredElements(c => c.Active).ToList();
            }

            return specialists;
        }

        public List<Video> GetPagedActiveElements(int pageIndex, int pageSize, string keyword, out int totalCount)
        {

            //get list count
            var keyCount = $"{EntityNames.Video}_ActiveListCount_{keyword}";

            var totalIQuery = _specialistRepository.GetAllElements().Where(m => m.Active);
            if (!string.IsNullOrEmpty(keyword))
                totalIQuery = totalIQuery.Where(g => g.Title.Contains(keyword));


            if (SettingsManager.Video.EnableCaching)
            {
                if (_cacheService.IsSet(keyCount))
                {
                    totalCount = (int)_cacheService.Get(keyCount);
                }
                else
                {
                    totalCount = totalIQuery.Count();
                    _cacheService.Set(keyCount, totalCount, SettingsManager.Video.CacheDuration);
                }
            }
            else
            {
                totalCount = totalIQuery.Count();
            }


            //get list
            var key = $"{EntityNames.Video}_ActiveList_{pageIndex}_{pageSize}_{keyword}";

            List<Video> specialist;
            Expression<Func<Video, bool>> filter = g => g.Active;
            Expression<Func<Video, bool>> filterByKeyword = g => g.Title.Contains(keyword);


            if (!string.IsNullOrEmpty(keyword))
                filter = filter.AndAlso(filterByKeyword);



            if (SettingsManager.Video.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    specialist = (List<Video>)_cacheService.Get(key);

                }
                else
                {
                    specialist = _specialistRepository.GetPagedElements(pageIndex, pageSize, (c => c.CreatedDate), filter, false).ToList();
                    _cacheService.Set(key, specialist, SettingsManager.Video.CacheDuration);
                }
            }
            else
            {
                specialist = _specialistRepository.GetPagedElements(pageIndex, pageSize, (c => c.CreatedDate), filter, false).ToList();
            }

            return specialist;

        }
        public List<Video> GetPagedElements(int pageIndex, int pageSize, string keyword, out int totalCount)
        {

            //get list count
            var keyCount = $"{EntityNames.Video}_ListCount_{keyword}";

            var totalIQuery = _specialistRepository.GetAllElements();
            if (!string.IsNullOrEmpty(keyword))
                totalIQuery = totalIQuery.Where(g => g.Title.Contains(keyword));
         

            if (SettingsManager.Video.EnableCaching)
            {
                if (_cacheService.IsSet(keyCount))
                {
                    totalCount = (int)_cacheService.Get(keyCount);
                }
                else
                {
                    totalCount = totalIQuery.Count();
                    _cacheService.Set(keyCount, totalCount, SettingsManager.Video.CacheDuration);
                }
            }
            else
            {
                totalCount = totalIQuery.Count();
            }


            //get list
            var key = $"{EntityNames.Video}_List_{pageIndex}_{pageSize}_{keyword}";

            List<Video> specialist;
            Expression<Func<Video, bool>> filter = g => true;
            Expression<Func<Video, bool>> filterByKeyword = g => g.Title.Contains(keyword);
         

            if (!string.IsNullOrEmpty(keyword))
                filter = filter.AndAlso(filterByKeyword);
           


            if (SettingsManager.Video.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    specialist = (List<Video>)_cacheService.Get(key);

                }
                else
                {
                    specialist = _specialistRepository.GetPagedElements(pageIndex, pageSize, (c => c.CreatedDate), filter, false).ToList();
                    _cacheService.Set(key, specialist, SettingsManager.Video.CacheDuration);
                }
            }
            else
            {
                specialist = _specialistRepository.GetPagedElements(pageIndex, pageSize, (c => c.CreatedDate), filter, false).ToList();
            }

            return specialist;

        }

        public IEnumerable<Video> LatesterVideos(int count)
        {
            var key = $"{EntityNames.Video}s_Latester_{count}";

            if (!SettingsManager.Video.EnableCaching)
            {
                return _specialistRepository.GetPagedElements(0, count, p => p.CreatedDate, p => p.Active, false).ToList();
            }

            if (_cacheService.IsSet(key))
            {
                return (List<Video>)_cacheService.Get(key);

            }

            var specialistList = _specialistRepository.GetPagedElements(0, count, p => p.CreatedDate, p => p.Active, false).ToList();
            _cacheService.Set(key, specialistList, SettingsManager.Video.CacheDuration);


            return specialistList;
        }
    }
}
